package yc.star.system.service.impl;


import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yc.star.common.domain.AppResult;
import yc.star.common.security.exception.ApplicationException;
import yc.star.system.domain.user.User;
import yc.star.system.domain.user.dto.UserDTO;
import yc.star.system.domain.user.dto.UserQueryDTO;
import yc.star.system.domain.user.vo.UserVO;
import yc.star.system.manager.UserCacheManager;
import yc.star.system.mapper.UserMapper;
import yc.star.system.service.IUserService;

import java.util.List;

@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserCacheManager userCacheManager;


    @Override
    public List<UserVO> list(UserQueryDTO userQueryDTO) {
        PageHelper.startPage(userQueryDTO.getPageNum(),userQueryDTO.getPageSize());
        List<UserVO> userVOList = userMapper.selectUserList(userQueryDTO);
        return userVOList;
    }

    @Override
    public int updateStatus(UserDTO userDTO) {
        // 判断该用户是否存在
        User user = userMapper.selectById(userDTO.getUserId());
        if (user == null) {
            throw new ApplicationException(AppResult.failed("用户不存在"));
        }
        user.setStatus(userDTO.getStatus());
        // 更新缓存
        userCacheManager.updateStatus(user.getUserId(),user.getStatus());
        return userMapper.updateById(user);
    }

}